package remote.controls
{
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	import remote.geom.EdgeMetrics;
	import remote.geom.Size;
	import remote.skins.SolidRectangleSkin;
	
	/**
	 * 多行可编辑文本框
	 * @author 银魂飞雪
	 * @createDate 2011-4-4
	 */
	public class TextArea extends Scroller
	{
		private var _textField:Text;
		public function TextArea()
		{
			super();
		}
		/**
		 * @inheritDoc 
		 * 
		 */
		override protected function createChildren():void
		{
			super.createChildren();
			_textField = new Text();
			_textField.textField.addEventListener(Event.CHANGE, textField_changeHandler);
			_textField.skin = new SolidRectangleSkin();
			_textField.textField.type = TextFieldType.INPUT;
			_textField.textField.multiline = true;
			_textField.textField.wordWrap = false;

			this.content = _textField;
		}
		
		private function textField_changeHandler(e:Event):void
		{
			updateChildren();
			if(_textField.textField.scrollH > 0)
				_textField.textField.scrollH = 0;
			if(_textField.textField.scrollV > 0)
				_textField.textField.scrollV = 0;
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */
		override protected function createSkin():void
		{
			//_skin = new SolidRectangleSkin();
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */
		override protected function updateChildren():void
		{
			super.updateChildren();
			if(_textField != null)
			{
				var edge:EdgeMetrics = _layout.edge;
				var textMearSize:Size = _textField.getMeasureSize();
				if(!isNaN(_explicitWidth))
				{
					var needWidth:Number = _explicitWidth - edge.left - edge.right;
					if(vscroller.visible == true)
					{
						needWidth -= vscroller.width;
					}
					if(autoWrap || textMearSize.width < needWidth)
						_textField.width = needWidth;
					else
						_textField.width = textMearSize.width;
				}
				if(!isNaN(_explicitHeight))
				{
					var needHeight:Number = _explicitWidth - edge.left - edge.right;
					if(hscroller.visible == true)
					{
						needHeight -= hscroller.height;
					}
					if(textMearSize.height < needHeight)
						_textField.height = needHeight;
					else
						_textField.height = textMearSize.height;
				}
				//更新滚动位置
				if(this.scrollX > this.maxScrollX || scrollY > maxScrollY)
				{
					scroll(maxScrollX, maxScrollY);
				}
					
			}
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */
		override public function getMeasureSize():Size
		{
			return _textField.getMeasureSize();
		}
		
		/**
		 * 是否自动换行 
		 * 
		 */		
		public function get autoWrap():Boolean
		{
			return _textField.textField.wordWrap;
		}
		
		public function set autoWrap(value:Boolean):void
		{
			if(value != autoWrap)
			{
				_textField.textField.wordWrap = value;
				this.updateChildren();
			}
		}

		/**
		 * 文本框，设置texfField属性，可通过此属性设置
		 * 
		 */		
		public function get textField():TextField
		{
			return _textField.textField;
		}
				
		
	}
}